/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.patpro.controladores;

import com.patpro.modelo.javabeans.SemestreAcademico;
import com.patpro.modelo.servicios.SemestreAcademicoService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

/** 
 
 */
public class AdminSemestreController extends SimpleFormController {

    private SemestreAcademicoService servicio_semestre;

    public void setServicio_semestre(SemestreAcademicoService servicio_semestre) {
        this.servicio_semestre = servicio_semestre;
    }
    

    public AdminSemestreController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        setCommandClass(SemestreAcademico.class);
        setCommandName("Semestrebean");
        //setSuccessView("successView");
        //setFormView("formView");
    }

    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
        SemestreAcademico s = (SemestreAcademico) super.formBackingObject(request);

        String id = request.getParameter("id");
        if(id != null)
            s = servicio_semestre.BuscarSemestre(Integer.parseInt(id));

        return s;
    }

    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map modelo = new HashMap();
        List<SemestreAcademico> semestres = servicio_semestre.listarSemestres();
        modelo.put("lista_semestres", semestres);

        String id = request.getParameter("id");
        if(id == null)
            modelo.put("opcion", "Agregar");
        else
            modelo.put("opcion", "Actualizar");
        return modelo;
    }
    

    
    @Override
    protected ModelAndView onSubmit(
            HttpServletRequest request, 
            HttpServletResponse response, 
            Object command, 
            BindException errors) throws Exception {
        ModelAndView mv = null;
        //Do something...
        SemestreAcademico semestre = (SemestreAcademico)command;
        int resp = servicio_semestre.actualizarSemestre(semestre);
        if(resp>0)
             mv=new ModelAndView(getSuccessView());
        else
            mv= new ModelAndView("error");
        mv.addObject("controlador", "adminSemestre");
        return mv;
    }
   

}